/*
 *    Copyright (c) 2018-2025, boss All rights reserved.
 *
 * Redistribution and use in source and binary forms, with or without
 * modification, are permitted provided that the following conditions are met:
 *
 * Redistributions of source code must retain the above copyright notice,
 * this list of conditions and the following disclaimer.
 * Redistributions in binary form must reproduce the above copyright
 * notice, this list of conditions and the following disclaimer in the
 * documentation and/or other materials provided with the distribution.
 * Neither the name of the boss developer nor the names of its
 * contributors may be used to endorse or promote products derived from
 * this software without specific prior written permission.
 * Author: boss
 */
package com.hopes.bizz.close.visitor.service.impl;

import cn.hutool.core.util.ObjUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.hopes.bizz.close.common.constant.VisitorDicConstant;
import com.hopes.bizz.close.common.constant.enums.ParkCloseModDictEnum;
import com.hopes.bizz.close.common.constant.enums.ParkCloseSysDictEnum;
import com.hopes.bizz.close.visitor.dto.ClosedGeneralCarRecordExcelDTO;
import com.hopes.bizz.close.visitor.entity.ClosedGeneralCarRecordEntity;
import com.hopes.bizz.close.visitor.mapper.ClosedGeneralCarRecordMapper;
import com.hopes.bizz.close.visitor.service.ClosedGeneralCarRecordService;
import com.hopes.bizz.close.visitor.vo.ClosedGeneralCarRecordExcelVO;
import com.hopes.bizz.close.visitor.vo.ClosedGeneralCarRecordVO;
import com.hopes.boss.admin.api.resolver.DictResolver;
import com.hopes.boss.admin.api.resolver.enums.AdminDictEnum;
import com.hopes.boss.common.entity.ValidateImportBO;
import com.hopes.boss.common.excel.vo.ErrorMessage;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;
import java.util.Optional;

/**
 * 普通车辆进出台账
 *
 * @author pig
 * @date 2023-10-26 14:08:35
 */
@Service
public class ClosedGeneralCarRecordServiceImpl extends ServiceImpl<ClosedGeneralCarRecordMapper, ClosedGeneralCarRecordEntity> implements ClosedGeneralCarRecordService {

    @Override
    public ClosedGeneralCarRecordVO getById(Long id) {
        if (ObjUtil.isNull(id)) {
            return null;
        }
        final ClosedGeneralCarRecordEntity closedGeneralCarRecordEntity = getBaseMapper().selectById(id);

        final ClosedGeneralCarRecordVO recordVO = new ClosedGeneralCarRecordVO();
        BeanUtils.copyProperties(closedGeneralCarRecordEntity, recordVO);
        if (recordVO.getDirection().equals(VisitorDicConstant.CLOSE_VISITOR_DIRECTION_OUT)) {
            final LambdaQueryWrapper<ClosedGeneralCarRecordEntity> queryWrapper = Wrappers.<ClosedGeneralCarRecordEntity>lambdaQuery()
                    .eq(ClosedGeneralCarRecordEntity::getDirection, VisitorDicConstant.CLOSE_VISITOR_DIRECTION_IN)
                    .eq(ClosedGeneralCarRecordEntity::getCarId, recordVO.getCarId())
                    .orderByDesc(ClosedGeneralCarRecordEntity::getInOutTime);
            final ClosedGeneralCarRecordEntity lastRecord = getBaseMapper().selectOne(queryWrapper);
            recordVO.setLastInOutTime(Optional.ofNullable(lastRecord).map(ClosedGeneralCarRecordEntity::getInOutTime).orElse(null));
        }
        return recordVO;
    }

    @Override
    public ValidateImportBO<ClosedGeneralCarRecordExcelDTO> validateImport(List<ClosedGeneralCarRecordExcelDTO> excelDTOList) {
        ValidateImportBO<ClosedGeneralCarRecordExcelDTO> bo = new ValidateImportBO<>();
        List<ErrorMessage> errorMessageList = new ArrayList<>();
        List<ClosedGeneralCarRecordExcelDTO> saveList = new ArrayList<>(excelDTOList);
        int successNum = excelDTOList.size();
        int failedNum = 0;

        bo.setErrorMessages(errorMessageList);
        bo.setSaveList(saveList);
        bo.setSuccessNum(successNum);
        bo.setFailedNum(failedNum);
        return bo;
    }

    @Override
    public ClosedGeneralCarRecordEntity importDtoToEntity(ClosedGeneralCarRecordExcelDTO excelDto) {
        ClosedGeneralCarRecordEntity entity= new ClosedGeneralCarRecordEntity();
        BeanUtils.copyProperties(excelDto, entity);
        return entity;
    }

    @Override
    public ClosedGeneralCarRecordExcelVO exportEntityToVo(ClosedGeneralCarRecordEntity entity) {
        ClosedGeneralCarRecordExcelVO excelVO = new ClosedGeneralCarRecordExcelVO();
        BeanUtils.copyProperties(entity, excelVO);

        excelVO.setOrgIdStr(DictResolver.getItemLabelByValue(AdminDictEnum.sys_company, entity.getOrgId()));
        excelVO.setGateIdStr(DictResolver.getItemLabelByValue(ParkCloseModDictEnum.closed_gate, entity.getGateId()));
        excelVO.setCarTypeStr(DictResolver.getItemLabelByValue(ParkCloseSysDictEnum.close_general_car_type, entity.getCarType()));
        excelVO.setPassTypeStr(DictResolver.getItemLabelByValue(ParkCloseSysDictEnum.close_general_car_pass_type, entity.getPassType()));
        excelVO.setDirectionStr(DictResolver.getItemLabelByValue(ParkCloseSysDictEnum.close_visitor_record_direction_type, entity.getDirection()));
        return excelVO;
    }
}
